package com.zyz.common.aspect;

import com.zyz.common.utils.RequestLogUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求和响应日志
 */
@Aspect
@Component
public class GlobalRequestAspect {
    @Autowired
    private HttpServletRequest request;

    private final static String el = "@annotation(org.springframework.web.bind.annotation.RequestMapping)";

    @Before(GlobalRequestAspect.el)
    public void before() {
    }

    @Around(GlobalRequestAspect.el)
    public Object around(ProceedingJoinPoint p) throws Throwable {
        Object[] args = p.getArgs();
        Object result = p.proceed();
        String path = request.getServletPath();
        if (!"/".equals(path)) {
            RequestLogUtils.logRequestAndResponse(request, args, result);
        }
        return result;
    }

    @After(GlobalRequestAspect.el)
    public void after() {
    }
}
